import { getRoute } from "@/api/route";
const state = {
  routes: []
};

// actions
const actions = {
  async init({ dispatch }) {
    const routes = await getRoute();
    await dispatch("set", routes);
  },

  set({ state, dispatch }, routes) {
    state.routes = routes;
    // 持久化
    return dispatch(
      "db/set",
      {
        dbName: "sys",
        path: "routes.routes",
        user: true,
        value: routes
      },
      { root: true }
    );
  },

  async load({ state, dispatch }) {
    const routes = dispatch(
      "db/get",
      {
        dbName: "sys",
        path: "routes.routes",
        defaultValue: {},
        user: true
      },
      { root: true }
    );
    state.routes = routes;
    return routes;
  }
};

export default {
  namespaced: true,
  state,
  actions
};
